#include "IWindow.h"

bool IWindow::UserInput::keys[256];
unsigned int IWindow::UserInput::mouseButtons;
Maths::Point<> IWindow::UserInput::mousePosition;

IWindow::IWindow()
{
	m_handle = 0;
	m_instance = 0;
	m_mouseDown = false;
}

void IWindow::ReceiveMessage( IEvent& event )
{
	EventHandler::Map_t::iterator it = m_eventHandlers.find(event.Type());
	if ( it == m_eventHandlers.end() || it->first != event.Type() )
		return;
	std::pair<EventHandler::Map_t::iterator, EventHandler::Map_t::iterator> ret = m_eventHandlers.equal_range(event.Type());
    for (it = ret.first; it != ret.second; ++it)
	{
		 if ((*it->second)(event))				// event consumed
			 break;
		 // else continue
	}
}

void IWindow::RegisterHandler(const HashedString& eventType, Pointer<EventHandler> handler)
{
	m_eventHandlers.insert(EventHandler::Pair_t( eventType, handler ));
}

void IWindow::RemoveHandler(const HashedString& eventType, void* handler)
{
	std::pair<EventHandler::Map_t::iterator, EventHandler::Map_t::iterator> ret = m_eventHandlers.equal_range(eventType);
    for (EventHandler::Map_t::iterator it = ret.first; it!= ret.second;)
	{
		if((*it->second).getPointer() == handler)
		{
			it = m_eventHandlers.erase(it);
		} else
		{
			++it;
		}
	}
}